package com.ruoyi.mp.controller;

import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.db.domain.system.MpClient;
import com.ruoyi.db.domain.system.MpClientRaiseRecord;
import com.ruoyi.db.domain.web.MpClientBank;
import com.ruoyi.db.domain.web.dto.MpBank;
import com.ruoyi.mp.annotion.Login;
import com.ruoyi.mp.annotion.LoginUser;
import com.ruoyi.mp.service.IMpClientBankService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 用户银行卡Controller
 * 
 * @author ruoyi
 * @date 2022-08-01
 */
@Api(value = "用户银行卡",tags ="用户银行卡")
@RestController
@RequestMapping("/smp/bank")
public class MpClientBankController extends BaseController
{
    private static Logger log = LoggerFactory.getLogger(MpClientBankController.class);
    @Autowired
    private IMpClientBankService mpClientBankService;

    /**
     * 查询用户银行卡列表
     */
    @Login
    @ApiOperation(value = "查询用户银行卡列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name="mpClientBank",value="用户银行卡",paramType="form")
    })
    @GetMapping("/list")
    public AjaxResult list(MpClientBank mpClientBank, @LoginUser MpClient client)
    {
        try {
            mpClientBank.setClientId(client.getClientId());
            List<MpClientBank> list = mpClientBankService.selectMpClientBankList(mpClientBank);
            Map<String, List<MpClientBank>> map = list.stream()
                    .collect(Collectors.groupingBy(MpClientBank::getIdNumber));
            List<MpClientBank> list1 = new ArrayList<>();
            for (String idNum: map.keySet()) {
                List<MpClientBank> banks = map.get(idNum);
                MpClientBank bank = new MpClientBank();
                bank=banks.get(0);
                List<MpBank> banks1  = banks.stream().map(e->{
                    MpBank mpBank = new MpBank();
                    mpBank.setId(e.getId());
                    mpBank.setBankNumber(e.getBankNumber());
                    return mpBank;
                }).collect(Collectors.toList());
                bank.setBanks(banks1);
                list1.add(bank);
            }
            return AjaxResult.success(list1);
        }catch (Exception e){
            log.error("用户银行卡发生异常",e);
        }
        return null;
    }


    /**
     * 获取用户银行卡详细信息
     */
    @Login
    @ApiOperation(value = "获取用户银行卡详细信息")
    @ApiImplicitParam(name="id",value="用户银行卡")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        try {
            return AjaxResult.success(mpClientBankService.selectMpClientBankById(id));
        }catch (Exception e){
            log.error("用户银行卡发生异常",e);
        }
        return AjaxResult.error("用户银行卡异常");
    }

    /**
     * 新增用户银行卡
     */
    @Login
    @ApiOperation(value = "新增用户银行卡")
    @ApiImplicitParams({
            @ApiImplicitParam(name="mpClientBank",value="用户银行卡",dataType="MpClientBank")
    })
    @Log(title = "用户银行卡", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MpClientBank mpClientBank,@LoginUser MpClient client)
    {
        try {
            if(StringUtils.isBlank(mpClientBank.getBankAccountName())){
                return AjaxResult.error("姓名不能为空");
            }
            if(StringUtils.isBlank(mpClientBank.getIdNumber())){
                return AjaxResult.error("身份证号不能为空");
            }
            if(StringUtils.isBlank(mpClientBank.getBankMobile())){
                return AjaxResult.error("手机号不能为空");
            }
            if(mpClientBank.getBankNumbers()==null||mpClientBank.getBankNumbers().size()<=0){
                return AjaxResult.error("银行卡号不能为空");
            }
            mpClientBank.setClientId(client.getClientId());
            int i=mpClientBankService.insertMpClientBank(mpClientBank,client.getMobile());
            return AjaxResult.success("添加成功");
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 修改用户银行卡
     */
    @Login
    @ApiOperation(value = "修改用户银行卡")
    @ApiImplicitParams({
            @ApiImplicitParam(name="mpClientBank",value="用户银行卡",dataType="MpClientBank")
    })
    @Log(title = "用户银行卡", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MpClientBank mpClientBank)
    {
        try {
            int i=mpClientBankService.updateMpClientBank(mpClientBank);
            return toAjax(i);
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
    }
    /**
     * 修改用户银行卡
     */
    @Login
    @ApiOperation(value = "修改用户银行卡")
    @ApiImplicitParams({
            @ApiImplicitParam(name="mpClientBank",value="用户银行卡",dataType="MpClientBank")
    })
    @Log(title = "删除账户", businessType = BusinessType.UPDATE)
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable Long id,@LoginUser MpClient client)
    {
        try {
            int i=mpClientBankService.deleteMpClientBankById(id,client.getClientId());
            return toAjax(i);
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
    }


}
